<template>
    <view class="content">
        <image @tap.stop.prevent="konck" class="bg w100pr h100pr" src="/static/images/index/fz.png"></image>
        <view class="oper flex flex-jsb flex-ac">
            <view class="center">
                <view class="title">电子念珠</view>
                <view class="count">{{ score }}</view>
            </view>
        </view>
        <view style="padding: 250rpx">
            <swiper @change="change" :circular="true" current="0" displayMultipleItems="8" duration="100" vertical="ture">
                <swiper-item v-for="(item, index) in beads" :key="index">
                    <image class="slide-image" :src="beadimg"></image>
                </swiper-item>
            </swiper>
        </view>
        <view class="tool">
            <view @tap="openaudio" class="tool-list">
                <view>
                    <image mode="widthFix" :src="audio ? '/static/images/audio.png' : '/static/images/audio-n.png'"></image>
                </view>
                <view class="text">声音</view>
            </view>
            <view @tap="refreshClick" class="tool-list">
                <view>
                    <image mode="widthFix" src="/static/images/reset.png"></image>
                </view>
                <view class="text">重置</view>
            </view>
            <view @tap="geClick" class="tool-list">
                <view>
                    <image mode="widthFix" src="/static/images/color.png"></image>
                </view>
                <view class="text">皮肤</view>
            </view>
        </view>
        <view :animation="agd1" class="gongde2">功德 + 1</view>
        <view :animation="agd2" class="gongde2">功德 + 1</view>
        <view :animation="agd3" class="gongde2">功德 + 1</view>
        <view :animation="agd4" class="gongde2">功德 + 1</view>
        <view :animation="agd5" class="gongde2">功德 + 1</view>
        <view :animation="agd6" class="gongde2">功德 + 1</view>
        <view :animation="agd7" class="gongde2">功德 + 1</view>
        <view :animation="agd8" class="gongde2">功德 + 1</view>
        <view :animation="agd9" class="gongde2">功德 + 1</view>
        <view :animation="agd0" class="gongde2">功德 + 1</view>
        <button class="share-btn" hoverClass="none" openType="share">
            <image class="w100pr h100pr" src="/static/images/share-btn.png"></image>
        </button>
    </view>
</template>

<script>
var t;
var e;
t = '';
e = '';
export default {
    data() {
        return {
            beads: [{}, {}, {}, {}, {}, {}, {}, {}, {}],
            i: 1,
            current: 0,
            firstIndex: 0,
            lastIndex: 6,
            playing: true,
            score: 0,
            scoreAnimateData: null,
            score_animated: false,
            htuserInfo: {},
            audio: true,
            audioContext: null,
            scoreAnimation: null,
            beadimg: '',
            o: '',
            agd1: '',
            agd2: '',
            agd3: '',
            agd4: '',
            agd5: '',
            agd6: '',
            agd7: '',
            agd8: '',
            agd9: '',
            agd0: ''
        };
    },
    onLoad: function (a) {
        this.onLoadClone3389(a);
    },
    onShow: function () {
        uni.showShareMenu({
            withShareTicket: false,
            menus: ['shareAppMessage', 'shareTimeline']
        });
        if (uni.getStorageSync('fz' + this.getCurrentDate())) {
            console.log(uni.getStorageSync('fz' + this.getCurrentDate()));
            this.setData({
                score: uni.getStorageSync('fz' + this.getCurrentDate())
            });
        } else {
            for (let t = -1; t > -3; t--) {
                uni.removeStorageSync('fz' + this.getCurrentDate(t));
            }
        }
    },
    onShareAppMessage: function () {
        return {
            title: uni.getStorageSync('fz' + this.getCurrentDate())
                ? '今日我已功德+' + uni.getStorageSync('fz' + this.getCurrentDate()) + '，你也一起来用电子念珠吧！'
                : '我发现了一款不错的解压神器！快来试一试吧！',
            path: 'pages/fozhu/index',
            imageUrl: '/static/res/share.jpg'
        };
    },
    onShareTimeline: function () {
        return {
            title: uni.getStorageSync('fz' + this.getCurrentDate())
                ? '今日我已功德+' + uni.getStorageSync('fz' + this.getCurrentDate()) + '，你也一起来用电子念珠吧！'
                : '我发现了一款不错的解压神器！快来试一试吧！',
            imageUrl: '/static/res/share.jpg',
            query: {
                path: 'fozhu'
            }
        };
    },
    onAddToFavorites: function (t) {
        return {
            title: '电子念珠',
            imageUrl: '/static/res/share.jpg',
            query: {
                path: 'fozhu'
            }
        };
    },
    methods: {
        onLoadClone3389: function (a) {
            var that = this;
            console.log(a);
            if (getApp().globalData.videoid && uni.createRewardedVideoAd) {
                (e = uni.createRewardedVideoAd({
                    adUnitId: getApp().globalData.videoid
                })).onLoad(function () {});
                e.onError(function (t) {});
                e.onClose(function (t) {
                    if (t && t.isEnded) {
                        that.overvideo();
                    } else {
                        uni.showToast({
                            title: '完整看完才能解锁哦~',
                            icon: 'none'
                        });
                    }
                });
            }
            if (getApp().globalData.interid) {
                if (uni.createInterstitialAd) {
                    (t = uni.createInterstitialAd({
                        adUnitId: getApp().globalData.interid
                    })).onLoad(function () {}),
                        t.onError(function (t) {}),
                        t.onClose(function () {});
                }
                setTimeout(function () {
                    if (t) {
                        t.show().catch(function (t) {
                            console.error(t);
                        });
                    }
                }, 2000);
            }
            if ((o = uni.getStorageSync('i'))) {
                this.setData({
                    beadimg: '/static/res/' + o + '.png'
                });
            } else {
                this.setData({
                    beadimg: '/static/res/' + this.i + '.png'
                });
            }
			let audio_src = "/static/audio/sound.mp3";
			// #ifdef MP-TOUTIAO
			audio_src = 'https://yunke.ssl.memorialsong.top/uploads/audio/sound.mp3';
			// #endif
			
            this.innerAudioContext = uni.createInnerAudioContext({
                useWebAudioImplement: true
            });
            this.innerAudioContext.src = audio_src;
            for (var o = 0; o < 10; o++) {
                this['agd' + o] = uni.createAnimation({
                    duration: 250,
                    transformOrigin: 'right top',
                    timingFunction: 'ease-out'
                });
            }
        },

        play: function () {
            this.innerAudioContext.pause
                ? (this.innerAudioContext.play(),
                  this.setData({
                      playing: true
                  }))
                : (this.innerAudioContext.stop(),
                  this.setData({
                      playing: false
                  }));
        },

        openaudio: function () {
            this.setData({
                audio: !this.audio
            });
        },

        refreshClick: function (t) {
            var that = this;
            uni.showModal({
                title: '提示',
                content: '是否重置数据？',
                success: function (t) {
                    t.confirm
                        ? (console.log('用户点击确定'),
                          uni.reportEvent('refresh', {
                              yesno: 'yes'
                          }),
                          that.setData({
                              score: 0
                          }),
                          uni.getStorageSync('fz' + that.getCurrentDate()) && uni.removeStorageSync('fz' + that.getCurrentDate()))
                        : t.cancel &&
                          (console.log('用户点击取消'),
                          uni.reportEvent('refresh', {
                              yesno: 'no'
                          }));
                }
            });
        },

        overvideo: function () {
            uni.setStorageSync('fz-key', this.getCurrentDate());
            if (this.i < 5) {
                var t = this.i + 1;
                this.setData({
                    i: t,
                    beadimg: '/static/res/' + t + '.png'
                });
                uni.setStorage({
                    key: 'i',
                    data: t
                });
            } else {
                t = 1;
                this.setData({
                    i: t,
                    beadimg: '/static/res/' + t + '.png'
                });
                uni.setStorage({
                    key: 'i',
                    data: t
                });
            }
        },

        geClick() {
            var that = this;
            if (uni.getStorageSync('fz-key') && uni.getStorageSync('fz-key') == this.getCurrentDate()) {
                this.overvideo();
                return false;
            }
            if (e) {
                uni.showModal({
                    title: '未解锁',
                    content: '看一段视频，可在今日随意切换佛珠皮肤',
                    confirmColor: '#439057',
                    success: function (a) {
                        if (a.confirm && e) {
                            e.show().catch(function () {
                                e.load()
                                    .then(function () {
                                        return e.show();
                                    })
                                    .catch(function (e) {
                                        console.log('激励视频 广告显示失败');
                                        if (that.i < 5) {
                                            var a = that.i + 1;
                                            that.setData({
                                                i: a,
                                                beadimg: '/static/res/' + a + '.png'
                                            });
                                            uni.setStorage({
                                                key: 'i',
                                                data: a
                                            });
                                        } else {
                                            a = 1;
                                            that.setData({
                                                i: a,
                                                beadimg: '/static/res/' + a + '.png'
                                            });
                                            uni.setStorage({
                                                key: 'i',
                                                data: a
                                            });
                                        }
                                    });
                            });
                        }
                    }
                });
            }
            if (that.i < 5) {
                var a = that.i + 1;
                that.setData({
                    i: a,
                    beadimg: '/static/res/' + a + '.png'
                });
                uni.setStorage({
                    key: 'i',
                    data: a
                });
            } else {
                a = 1;
                that.setData({
                    i: a,
                    beadimg: '/static/res/' + a + '.png'
                });
                uni.setStorage({
                    key: 'i',
                    data: a
                });
            }
        },

        getCurrentDate: function (t = 0) {
            const e = new Date();
            e.setDate(e.getDate() + t);
            const a = {
                year: e.getFullYear(),
                month: e.getMonth() + 1,
                date: e.getDate()
            };
            return a.year + '' + (a.month >= 10 ? a.month : '0' + a.month) + (a.date >= 10 ? a.date : '0' + a.date);
        },

        change: function (t) {
            var e = this.score + 1;
            if (this.audio) {
                this.innerAudioContext.play();
            }
            var a = '' + this.score;
            var n = a.substr('' + a.length - 1);
            console.log(n);
            var o = ['agd' + n];
            this['agd' + n].top('51%').step().top('40%').opacity(1).step(50).opacity(0).step().step().top('51%');
            this.setData({
                [o]: this['agd' + n].export()
            });
            this.setData({
                score: e
            });
            uni.setStorageSync('fz' + this.getCurrentDate(), e);
        },

        konck() {
            console.log('占位：函数 konck 未声明');
        }
    }
};
</script>
<style>
@import './index.css';
</style>
